package com.example.bookdemo.service;

import com.example.bookdemo.dao.BookInfoMapper;
import com.example.bookdemo.enums.GetStatusCN;
import com.example.bookdemo.model.PageResult;
import com.example.bookdemo.model.RequestPage;
import com.example.bookdemo.model.bookInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class bookService {
    //将对象从 Spring 里面取出
    @Autowired//表示取出对象
    private BookInfoMapper bookInfoMapper;

    //展示所有图书
     public List<bookInfo> getList(){
         //从数据库里面获取数据,调取Mapper
         List<bookInfo> bookInfos = bookInfoMapper.selectInfo();
         for (bookInfo bookInfo:bookInfos){
             bookInfo.setStateCN(GetStatusCN.getNameByCode(bookInfo.getStatus()).getName());
         }
        return bookInfos;
    }

    public PageResult<bookInfo> getListByPage(RequestPage requestPage){
         int count = bookInfoMapper.count();
         List<bookInfo> bookInfos = bookInfoMapper.getBookByPage(requestPage.getOffset(),requestPage.getPageSize());
         for (bookInfo bookInfo: bookInfos){
            bookInfo.setStateCN(GetStatusCN.getNameByCode(bookInfo.getStatus()).getName());
         }
         return new PageResult<>(bookInfos,count,requestPage);
    }

    //添加图书
    public Integer insertBook(bookInfo bookInfo){
        return bookInfoMapper.insertBook(bookInfo);
    }

    public bookInfo selectBookById(Integer id) {
         return bookInfoMapper.selectById(id);
    }

    public Integer updateBookInfoById(bookInfo bookInfo) {
         return bookInfoMapper.updateBookById(bookInfo);
    }

    public Integer deleteBookINfoById(Integer id) {
         return bookInfoMapper.deleteById(id);
    }

    public Integer batchDelete(List<Integer> ids) {
         return bookInfoMapper.batchDelete(ids);
    }
}
